// pages/main.js
const WXAPI = require('apifm-wxapi')
const AUTH = require('../../utils/auth')
const APP = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stores: [],
    pageNum: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getApp().initLanguage(this)
    wx.setNavigationBarTitle({
      title: this.data.$t.shop.select,
    })
    this.setData({
      type: options.type,
      shop_join_open: wx.getStorageSync('shop_join_open')
    })
  
    // this.loadStores();

    console.log("onShow")
    wx.getLocation({
      type: 'wgs84', //wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: (res) => {
        this.data.latitude = res.latitude
        this.data.longitude = res.longitude
        console.log("ddddddddd",res.latitude)
        this.fetchShops(res.latitude, res.longitude, '')
      },
      fail(e){
        console.log("checkAndAuthorize")
        console.error(e)
        AUTH.checkAndAuthorize('scope.userLocation')
      }
    }) 
  },

  async fetchShops(latitude, longitude, kw){
    const res = await WXAPI.fetchShops({
      curlatitude: latitude,
      curlongitude: longitude,
      nameLike: kw
    })
    console.log(res,"res")
    if (res.code == 0) {
      res.data.forEach(ele => {
        ele.distance = ele.distance.toFixed(1) // 距离保留3位小数
      })
      console.log("res.data",res.data)
      this.setData({
        shops: res.data
      })
    } else {
      this.setData({
        shops: null
      })
    }
  },

  goShop(e){
    console.log("eeee",e.currentTarget.dataset.data)
    const idx = e.id   
    wx.setStorageSync('shopInfo', e.currentTarget.dataset.data.id)
    wx.setStorageSync('refreshIndex', 1)
    wx.navigateTo({
      url: '/pages/index/index?id='+e.currentTarget.dataset.data.id
    })
  },

  goEdit(e){
    console.log("编辑门店",e.currentTarget.dataset.data)
    const storeData = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '/pages/store-manage/edit/edit?id='+storeData.id
    })
  },
  joinApply() {
    wx.navigateTo({
      url: '/pages/shop/join-apply',
    })
  },
  goCreateStore() {
    wx.navigateTo({
      url: '/pages/store-manage/create/create'
    })
  },

  /**
   * 点击店铺事件处理
   */
  onStoreTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/store/detail?id=${id}`
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 下拉刷新，重置数据并重新加载
    this.setData({
      stores: [],
      pageNum: 1,
      noMoreData: false
    }, () => {
  
      // this.loadStores();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 上拉加载更多
    if (!this.data.isLoading && !this.data.noMoreData) {
      // this.loadStores();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '发现好店，品味生活',
      path: '/pages/main/index'
    };
  }
})